/***********************************************************************
 * Copyright CSAir.
 * All rights reserved. 
 *
 * HISTORY
 ***********************************************************************
 *  ID      DATE                PERSON            REASON
 *  1		2009-10-07			叶志明			创建文件
 *  2		2009-10-29			李春林			修改loadData方法，使其支持整型字段
 ************************************************************************
 * 
 */
package com.redhat.redwork.widget.dropdown;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * a facility to build dropdowns by config
 * 
 * @author Justin Yip
 * 
 */
public class EntityDropdownDataModel implements DropdownDataModel {
	
	@Autowired
	private DataModelService dataModelService;
	
	/** entity class name */
	String className;

	/** property as dropdown label */
	String labelProperty;

	/** property as dropdown value */
	String valueProperty;

	/** property mapping to selected value of parent dropdown */
	String parentProperty;



	public void setClassName(String className) {
		this.className = className;
	}

	public void setLabelProperty(String labelProperty) {
		this.labelProperty = labelProperty;
	}

	public void setValueProperty(String valueProperty) {
		this.valueProperty = valueProperty;
	}

	public void setParentProperty(String parentProperty) {
		this.parentProperty = parentProperty;
	}

	// public void setUniversalDao(UniversalDao universalDao) {
	// this.universalDao = universalDao;
	// }

	public Map<String, String> loadData(String masterValue,
			Map<String, String> params) {

		// the dropdown data map to return
		Map<String, String> dropdownData = new TreeMap<String, String>();

		List<?> objects = dataModelService.loadData(className, parentProperty, masterValue);

		for (Object obj : objects) {
			BeanWrapper beanWrapper = new BeanWrapperImpl(obj);
			String keyStr = null;
			String valueStr = null;
			Object key = beanWrapper.getPropertyValue(labelProperty);
			if (key != null)
				keyStr = key.toString();
			Object value = beanWrapper.getPropertyValue(valueProperty);
			if (value != null)
				valueStr = value.toString();
			if (keyStr != null)
				dropdownData.put(keyStr, valueStr);
		}

		return dropdownData;
	}

	/**
	 * utility method to retrieve String value from parameter map
	 * 
	 * @param params
	 *            request parameter map
	 * @param name
	 *            value name
	 * @return the required String value, or null if not
	 */
	protected static String getValue(Map<String, String> params, String name) {
		Object stringArray = params.get(name);
		if (stringArray != null && stringArray instanceof String[]) {
			return ((String[]) stringArray)[0];
		}
		return null;
	}

	public DataModelService getDataModelService() {
		return dataModelService;
	}

	public void setDataModelService(DataModelService dataModelService) {
		this.dataModelService = dataModelService;
	}
}
